package com.ruoyi.purchase.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.PurchaseOrder;
import com.ruoyi.purchase.domain.PurchaseOrderParams;
import com.ruoyi.purchase.service.IOrderDetailService;
import com.ruoyi.purchase.service.IPurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单Controller
 *
 * @author kevinness
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/purchaseOrder")
public class PurchaseOrderController extends BaseController
{
    @Autowired
    private IPurchaseOrderService purchaseOrderService;

    @Autowired
    private IOrderDetailService orderDetailService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PurchaseOrder purchaseOrder)
    {
        startPage();
        List<PurchaseOrder> list = purchaseOrderService.selectPurchaseOrderList(purchaseOrder);
        return getDataTable(list);
    }

    /**
     * 采购执行查询订单列表
     */
    @GetMapping("/execution/list")
    public TableDataInfo getExecutionList(PurchaseOrder purchaseOrder){
        startPage();
        List<PurchaseOrder> list = purchaseOrderService.getExecutionPurchaseOrderList(purchaseOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PurchaseOrder purchaseOrder)
    {
        List<PurchaseOrder> list = purchaseOrderService.selectPurchaseOrderList(purchaseOrder);
        ExcelUtil<PurchaseOrder> util = new ExcelUtil<PurchaseOrder>(PurchaseOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{orderNo}")
    public AjaxResult getInfo(@PathVariable("orderNo") String orderNo)
    {
        return success(purchaseOrderService.selectPurchaseOrderByOrderNo(orderNo));
    }

    /**
     * 新增订单
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PurchaseOrder purchaseOrder)
    {
        return toAjax(purchaseOrderService.insertPurchaseOrder(purchaseOrder));
    }

    /**
     * 变更订单
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping("/operator")
    public AjaxResult change(@RequestBody PurchaseOrderParams purchaseOrderParams)
    {
        return toAjax(purchaseOrderService.updatePurchaseOrder(purchaseOrderParams,"运营商"));
    }

    /**
     * 变更订单
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping("/provider")
    public AjaxResult providerChange(@RequestBody PurchaseOrderParams purchaseOrderParams)
    {
        return toAjax(purchaseOrderService.updatePurchaseOrder(purchaseOrderParams,"供货商"));
    }

    /**
     * 修改订单
     */
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody PurchaseOrderParams purchaseOrderParams){
        int result = purchaseOrderService.updatePurchaseOrder(purchaseOrderParams);
        result = orderDetailService.updateOrderDetailList(purchaseOrderParams.getDetails()) < result ? 0 : result;
        return toAjax(result);
    }

    /**
     * 删除订单
     */
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderNos}")
    public AjaxResult remove(@PathVariable String[] orderNos)
    {
        for (String orderNo : orderNos){
            PurchaseOrder purchaseOrder =
                    purchaseOrderService.selectPurchaseOrderByOrderNo(orderNo);
            if(!purchaseOrder.getOrderStatus().equals("待确认")
                    && !purchaseOrder.getOrderStatus().equals("已入库")){
                return error("无法删除执行过程中的订单！");
            }
        }
        return toAjax(purchaseOrderService.deletePurchaseOrderByOrderNos(orderNos));
    }

    /**
     * 退货
     */
    @PostMapping("/return")
    public AjaxResult returnOrder(@RequestBody String orderNo){
        return toAjax(purchaseOrderService.returnPurchaseOrderByOrderNo(orderNo));
    }

    /**
     * 更新订单状态
     */
    @PostMapping("/update/status")
    public AjaxResult updateStatus(@RequestBody PurchaseOrder purchaseOrder){
        return toAjax(purchaseOrderService.updatePurchaseOrderStatus(purchaseOrder));
    }
}